/*
 * @Author: xin.yang 
 * @Date: 2019-10-22 14:20:45 
 * @Last Modified by: xin.yang
 * @Last Modified time: 2019-10-22 14:24:44
 */

const fs = require("fs");
const path = require("path");
const utils = require("./utils");


//得到所有相册
exports.getAllAlbums = callback => {
  fs.readdir("./uploads/", (err, files) => {
    if (err) {
      fs.mkdir("./uploads/", err => {
        if (err) return callback(err, { code: 2020, msg: "获取相册失败" });
        utils.repeatAllAlbums(files, callback);
      });
    } else {
      utils.repeatAllAlbums(files, callback);
    }
  });
};


//通过文件名，得到所有图片
exports.getAllImagesByAlbumName = (albumName, callback) => {
  fs.readdir("./uploads/" + albumName, (err, files) => {
    if (err) {
      fs.mkdir("./uploads/", err => {
        if (err) {
          return callback(err);
        }
        utils.repeatAllImage(files, albumName, callback);
      });
    } else {
      utils.repeatAllImages(files, albumName, callback);
    }
  });
};

//创建相册
exports.createdir = (data, callback) => {
  fs.readdir("./uploads", (err, files) => {
    if (err) callback(err, { code: 2020, msg: "获取相册失败" });
    const dir = files.find(value => {
      return value == data.dirname;
    });
    if (dir === undefined) {
      fs.mkdir("./uploads/" + data.dirname, err => {
        if (err) callback(err, { code: 1020, msg: "相册创建失败" });
        callback(null, { code: 0, msg: "相册创建成功" });
      });
    } else {
      callback(null, { code: 1010, msg: "相册已经存在" });
    }
  });
};

//删除相册
exports.removedir = (data, callback) => {
  let pathName = path.join(__dirname, `../uploads/${data.dirname}`.trim());
  fs.readdir(pathName, (err, files) => {
    if (err) {
      callback(err, { code: 1020, msg: "相册不存在" });
    } else if (files.length == 0) {
      fs.rmdir(pathName, err => {
        if (err) callback(err);
        callback(null, { code: 0, msg: "删除相册成功" });
      });
    } else {
      for (let i = 0, length = files.length; i < length; i++) {
        fs.unlink(path.join(pathName, files[i]), err => {
          if (err) {
            callback(err, { code: 1030, msg: "删除相册文件失败" });
          }
          if (i === files.length - 1) {
            fs.rmdir(pathName, err => {
              if (err) {
                callback(err, { code: 1030, msg: "删除相册失败" });
              }
              callback(null, { code: 0, msg: "删除相册成功" });
            });
          }
        });
      }
    }
  });
};

//重命名相册
exports.renamedir = (data, callback) => {
  let oldpath = path.join(__dirname, `../uploads/${data.oldname}`);
  let newpath = path.join(__dirname, `../uploads/${data.newname}`);
  fs.readdir("./uploads", (err, files) => {
    if (err) callback(err, { code: 2020, msg: "获取相册失败" });
    const dir = files.find(value => {
      return value == data.newname;
    });
    if (dir === undefined) {
      fs.rename(oldpath, newpath, err => {
        if (err) callback(err);
        callback(null, { code: 0, msg: "相册重命名成功" });
      });
    } else {
      callback(null, { code: 1010, msg: "相册名已经存在" });
    }
  });
};

//删除图片
exports.removefile = (data, callback) => {
  fs.unlink(
    path.join(__dirname, "../uploads/" + data.filedir + "/" + data.filename),
    err => {
      if (err) callback(err, { code: 1010, msg: "图片删除失败" });
      callback(null, { code: 0, msg: "图片删除成功" });
    }
  );
};
